package com.xapp.utils;

import android.app.Activity;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.xapp.R;


public class PopWindowUtils {

    public static void showPopUpWindow(final Activity context, View parent, final View.OnClickListener list1, final View.OnClickListener list2) {//展示意见反馈选择图片popwindow
        View view = LayoutInflater.from(context).inflate(R.layout.popupwindow_choose_image, null, false);
        RelativeLayout rl = view.findViewById(R.id.popupwindow_choose_image_rl);
        TextView tv1 = view.findViewById(R.id.popupwindow_choose_image_tv1);
        TextView tv2 = view.findViewById(R.id.popupwindow_choose_image_tv2);
        TextView tv3 = view.findViewById(R.id.popupwindow_choose_image_tv3);
        final PopupWindow popupWindow = new PopupWindow(context);
        popupWindow.setContentView(view);
        popupWindow.setBackgroundDrawable(new ColorDrawable(0));
        popupWindow.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        popupWindow.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
        popupWindow.setAnimationStyle(R.style.mypopwindow_anim_style_user_back);
        popupWindow.setClippingEnabled(false);
        rl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                darkenBackground(context, 1f);
                popupWindow.dismiss();
            }
        });
        tv1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                list1.onClick(v);
                darkenBackground(context, 1f);
                popupWindow.dismiss();
            }
        });
        tv2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                list2.onClick(v);
                darkenBackground(context, 1f);
                popupWindow.dismiss();
            }
        });
        tv3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                darkenBackground(context, 1f);
                popupWindow.dismiss();
            }
        });
        popupWindow.showAtLocation(parent, Gravity.BOTTOM, 0, 0);
        darkenBackground(context, 0.7f);
    }

    /**
     * 改变背景颜色
     */
    private static void darkenBackground(Activity context, Float bgcolor) {
        WindowManager.LayoutParams lp = context.getWindow().getAttributes();
        lp.alpha = bgcolor;

        context.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        context.getWindow().setAttributes(lp);

    }
}
